import {getRequest} from "@/utils/api";

export const initMenu=(router,store)=>{
    if(store.state.routes.length>0)
        return;
    getRequest('/system/cfg/menu').then(date=>{
        if(date){
            //格式化Router
            let fmtRoutes= formatRoutes(date);

            router.addRoutes(fmtRoutes);

            store.commit('initRoutes',fmtRoutes)

        }
    })
}
export const formatRoutes = (routes)=>{
    let fmtRoutes = [];
    routes.forEach(router=>{
        let {
            path,
            component,
            name,
            iconCls,
            children,
        }=router;
        if(children &&children instanceof Array){
            //递归
            children=formatRoutes(children);
        }
        let fmRouter={
            path:path,
            name:name,
            iconCls:iconCls,
            children:children,
            component(resolve){
                if(component.startsWith('Home')){
                    require(['../View/'+component+'.vue'],resolve);
                }
                else if(component.startsWith('Emp')){
                    require(['../View/emp/'+component+'.vue'],resolve);
                }
                else if(component.startsWith('Per')){
                    require(['../View/per/'+component+'.vue'],resolve);
                }
                else if(component.startsWith('Sal')){
                    require(['../View/sal/'+component+'.vue'],resolve);
                }
                else if(component.startsWith('Sta')){
                    require(['../View/sta/'+component+'.vue'],resolve);
                }
                else if(component.startsWith('Sys')){
                    require(['../View/sys/'+component+'.vue'],resolve);
                }

            }
        }
        fmtRoutes.push(fmRouter)
    });
    return fmtRoutes;
}